package com.mengchengnet.chaperonage.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.domain.base.Service;
import com.mengchengnet.chaperonage.util.PreferenceUtil;
import com.mengchengnet.chaperonage.util.Url;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/20.
 */

public class ServiceActivity extends BaseActivity {
    private TextView tv;
    private RequestQueue requestQueue;
    private List<Service> list=new ArrayList<>();
    String s="";
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    tv.setText("暂无");
                    break;
                case 0:
                    for (int i = 0; i <list.size() ; i++) {
                        s=s+"  "+list.get(i).getName()+"\n"+"  "+list.get(i).getContent()+"\n";
                    }
                    tv.setText(s);
                    break;
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("服务内容");
        tv= (TextView) findViewById(R.id.service_tv);
        requestQueue= Volley.newRequestQueue(getApplication());
        getSkill();
    }

    private void getSkill(){
        StringRequest request = new StringRequest(Request.Method.POST, Url.getCareInfo,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String result) {
                        Log.e("获取陪护技能信息", "获取陪护技能信息接口返回值："+result );
                        JSONObject jsonObject;
                        try {
                            if(result != null && result.startsWith("\ufeff"))
                            {
                                result =  result.substring(1);
                            }
                            jsonObject = new JSONObject(result);
                            if (jsonObject.getString("code").equals("00")){
                                JSONArray array=jsonObject.getJSONArray("skill_list");
                                Log.e("ppp", "集合长度: "+array.length());
                                if (array.length()>0){

                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject obj = array.getJSONObject(i);
                                        String name = obj.getString("name");
                                        String brief = obj.getString("brief");

                                        Service service = new Service();
                                        service.setName(name);
                                        service.setContent(brief);
                                        list.add(service);

                                    }
                                    handler.sendEmptyMessage(0);
                                }else {
                                    handler.sendEmptyMessage(1);
                                }
                            }
                            else {
                                Toast.makeText(ServiceActivity.this, "失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError arg0) {
                Toast.makeText(ServiceActivity.this, "网络异常请稍后重试！", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("version", PreferenceUtil.getString(getApplication(),"versionCode"));
                map.put("zescort_id", getIntent().getStringExtra("careId"));
                return map;
            }
        };
        request.setTag("GetFenZu");
        request.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, // 默认超时时间，应设置一个稍微大点儿的
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        requestQueue.add(request);

    }
}
